var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            GroupID: 0,
            openid: 0,
            BusinessID:0,
            Content: ''
        },
        list: [],
        getdata_timeout: null
    },
    methods: {
        get_data: function(can_scroll) {
            var that = this;
            var options={
              action:'getlivechatmsglistwithbusiness',
              GroupID: that.form.GroupID,
            };
            ns.post(options, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        that.list=data.List;
                        setTimeout(function() {
                            app.do_scroll();
                        }, 200);
                    }

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                // if (can_scroll) {
                    
                // }
                // that.getdata_timeout = setTimeout(function() {
                //     that.get_data(false);
                // }, 5000);
            })
        },
        do_scroll: function() {
            var h = document.documentElement.scrollHeight || document.body.scrollHeight;
            console.log(JSON.stringify(h));
            setTimeout(() => {
                        window.scrollTo(0,h);
                    }, 300);
        },
        start_post: function() {
            var that = this;
            setTimeout(function() {
                that.resize_textarea();
            }, 200);
        },
        resize_textarea: function() {
            var text = document.getElementById("textarea");
            autoTextarea(text, 0, 100);
        },
        do_post: function() {
            var that = this;
            if (that.form.Content == '') {
                return;
            }
            ns.post({
                action: 'businessreplyuserlivechatmsg',
                Content: that.form.Content,
                GroupID: that.form.GroupID,
                OpenID: that.form.openid,
                ChatType:1,
            }, function(succeed, data, err) {
                // if (that.getdata_timeout != null) {
                //     clearTimeout(that.getdata_timeout);
                // }
                that.get_data(true);
                if (succeed) {
                    that.do_send(that.form.Content);
                    that.form.Content = '';
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, { toast: true, toastmsg: '发送中' });
        },
        //给微信端发送消息通过的通知
        do_send: function (Content) {
            var that = this;
            // var message = "扫码已通过刷新列表";
            console.log(Content);
            //发送消息
            webSocket.sendSocketMessage({
                msg: {
                    messageType: 2,
                    guid:that.form.openid,
                    // guid:'tangjuan',
                    content: Content
                },
                success: function (res) {
                    // console.log(JSON.stringify(that.form.openid));
                    console.log(JSON.stringify(res));
                    // webSocket.closeSocket();
                }
            })
        },
        onSocketMessageCallback:function(){
            var that=this;
            that.get_data();
        },
        scrollToButtom:function() {
            var that=this;
            that.$nextTick(() => {
                let chatboxContainer = that.$refs.chatbox // 获取对
                if (chatboxContainer) {
                    window.scrollTo(0, chatboxContainer.scrollHeight);
                }
            })
        },

    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.GroupID = api.pageParam.id || 0;
    app.form.openid=api.pageParam.openid;
    app.form.BusinessID=api.pageParam.BusinessID;
    console.log(JSON.stringify(app.form.BusinessID));
    webSocket.guid = 'businessapp'+app.form.BusinessID;
    console.log(JSON.stringify( webSocket.guid));
    webSocket.closeSocket();
    setTimeout(function () {
        webSocket.connectSocket();
    }, 300);
    webSocket.onSocketMessageCallback = app.onSocketMessageCallback;
    toast = new auiToast();
    app.get_data(true);
    app.scrollToButtom();
    app.resize_textarea();
}
